using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using PickGold.Charting.ChartTypes;
using PickGold.Charting.Utilities;
using PickGold.Charting.Design;

namespace PickGold.Charting
{
	[DefaultProperty("Enabled"), SRDescription("DescriptionAttributeLegend_Legend"), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class Legend : ChartNamedElement
	{
		private int _autoFitFontSizeAdjustment;
		private int _autoFitMinFontSize;
		private Color _backColor;
		private GradientStyle _backGradientStyle;
		private ChartHatchStyle _backHatchStyle;
		private string _backImage;
		private ChartImageAlignmentStyle _backImageAlignment;
		private Color _backImageTransparentColor;
		private ChartImageWrapMode _backImageWrapMode;
		private Color _backSecondaryColor;
		private Color _borderColor;
		private ChartDashStyle _borderDashStyle;
		private int _borderWidth;
		private LegendCellColumnCollection _cellColumns;
		private int[,] _cellHeights;
		private LegendItemsCollection _customLegends;
		private string _dockedToChartArea;
		private bool _enabled;
		private System.Drawing.Font _font;
		private FontCache _fontCache;
		private Color _foreColor;
		private Rectangle _headerPosition;
		private LegendSeparatorStyle _headerSeparator;
		private Color _headerSeparatorColor;
		private int _horizontalSpaceLeft;
		private bool _interlacedRows;
		private Color _interlacedRowsColor;
		private bool _isDockedInsideChartArea;
		private bool _isEquallySpacedItems;
		private bool _isTextAutoFit;
		private int _itemColumns;
		private LegendSeparatorStyle _itemColumnSeparator;
		private Color _itemColumnSeparatorColor;
		private int _itemColumnSpacing;
		private int _itemColumnSpacingRel;
		private StringAlignment _legendAlignment;
		private Docking _legendDocking;
		private LegendItemOrder _legendItemOrder;
		private Rectangle _legendItemsAreaPosition;
		private bool _legendItemsTruncated;
		private LegendStyle _legendStyle;
		private LegendTableStyle _legendTableStyle;
		private float _maximumLegendAutoSize;
		private int _numberOfCells;
		private int _numberOfLegendItemsToProcess;
		private int[] _numberOfRowsPerColumn;
		private Size _offset;
		private ElementPosition _position;
		private Color _shadowColor;
		private int _shadowOffset;
		private int[,] _subColumnSizes;
		private int _textWrapThreshold;
		private string _title;
		private StringAlignment _titleAlignment;
		private Color _titleBackColor;
		private System.Drawing.Font _titleFont;
		private Color _titleForeColor;
		private Rectangle _titlePosition;
		private LegendSeparatorStyle _titleSeparator;
		private Color _titleSeparatorColor;
		private int _truncatedDotsSize;
		private int _verticalSpaceLeft;
		internal System.Drawing.Font autofitFont;
		internal LegendItemsCollection legendItems;
		internal Size singleWCharacterSize;

		public Legend()
		{
			this._enabled = true;
			this._legendStyle = LegendStyle.Table;
			this._backImage = "";
			this._backImageTransparentColor = Color.Empty;
			this._backSecondaryColor = Color.Empty;
			this._borderColor = Color.Empty;
			this._backColor = Color.Empty;
			this._borderWidth = 1;
			this._borderDashStyle = ChartDashStyle.Solid;
			this._fontCache = new FontCache();
			this._foreColor = Color.Black;
			this._legendDocking = Docking.Right;
			this._shadowColor = Color.FromArgb(0x80, 0, 0, 0);
			this._isTextAutoFit = true;
			this._dockedToChartArea = "NotSet";
			this._isDockedInsideChartArea = true;
			this._interlacedRowsColor = Color.Empty;
			this._offset = Size.Empty;
			this._maximumLegendAutoSize = 50f;
			this._textWrapThreshold = 0x19;
			this._title = string.Empty;
			this._titleForeColor = Color.Black;
			this._titleBackColor = Color.Empty;
			this._titleAlignment = StringAlignment.Center;
			this._titleSeparatorColor = Color.Black;
			this._headerSeparatorColor = Color.Black;
			this._itemColumnSeparatorColor = Color.Black;
			this._itemColumnSpacing = 50;
			this._titlePosition = Rectangle.Empty;
			this._headerPosition = Rectangle.Empty;
			this._autoFitMinFontSize = 7;
			this._numberOfLegendItemsToProcess = -1;
			this._legendItemsAreaPosition = Rectangle.Empty;
			this._truncatedDotsSize = 3;
			this._numberOfCells = -1;
			this.singleWCharacterSize = Size.Empty;
			this._position = new ElementPosition(this);
			this._customLegends = new LegendItemsCollection(this);
			this.legendItems = new LegendItemsCollection(this);
			this._cellColumns = new LegendCellColumnCollection(this);
			this._font = this._fontCache.DefaultFont;
			this._titleFont = this._fontCache.DefaultBoldFont;
		}

		public Legend(string name)
			: base(name)
		{
			this._enabled = true;
			this._legendStyle = LegendStyle.Table;
			this._backImage = "";
			this._backImageTransparentColor = Color.Empty;
			this._backSecondaryColor = Color.Empty;
			this._borderColor = Color.Empty;
			this._backColor = Color.Empty;
			this._borderWidth = 1;
			this._borderDashStyle = ChartDashStyle.Solid;
			this._fontCache = new FontCache();
			this._foreColor = Color.Black;
			this._legendDocking = Docking.Right;
			this._shadowColor = Color.FromArgb(0x80, 0, 0, 0);
			this._isTextAutoFit = true;
			this._dockedToChartArea = "NotSet";
			this._isDockedInsideChartArea = true;
			this._interlacedRowsColor = Color.Empty;
			this._offset = Size.Empty;
			this._maximumLegendAutoSize = 50f;
			this._textWrapThreshold = 0x19;
			this._title = string.Empty;
			this._titleForeColor = Color.Black;
			this._titleBackColor = Color.Empty;
			this._titleAlignment = StringAlignment.Center;
			this._titleSeparatorColor = Color.Black;
			this._headerSeparatorColor = Color.Black;
			this._itemColumnSeparatorColor = Color.Black;
			this._itemColumnSpacing = 50;
			this._titlePosition = Rectangle.Empty;
			this._headerPosition = Rectangle.Empty;
			this._autoFitMinFontSize = 7;
			this._numberOfLegendItemsToProcess = -1;
			this._legendItemsAreaPosition = Rectangle.Empty;
			this._truncatedDotsSize = 3;
			this._numberOfCells = -1;
			this.singleWCharacterSize = Size.Empty;
			this._position = new ElementPosition(this);
			this._customLegends = new LegendItemsCollection(this);
			this.legendItems = new LegendItemsCollection(this);
			this._cellColumns = new LegendCellColumnCollection(this);
			this._font = this._fontCache.DefaultFont;
			this._titleFont = this._fontCache.DefaultBoldFont;
		}

		internal void CalcLegendPosition(ChartGraphics chartGraph, ref RectangleF chartAreasRectangle, float elementSpacing)
		{
			RectangleF ef = new RectangleF();
			SizeF maxSizeRel = new SizeF(chartAreasRectangle.Width - (2f * elementSpacing), chartAreasRectangle.Height - (2f * elementSpacing));
			if (this.DockedToChartArea == "NotSet")
			{
				if ((this.Docking == Docking.Top) || (this.Docking == Docking.Bottom))
				{
					maxSizeRel.Height = (maxSizeRel.Height / 100f) * this._maximumLegendAutoSize;
				}
				else
				{
					maxSizeRel.Width = (maxSizeRel.Width / 100f) * this._maximumLegendAutoSize;
				}
			}
			if ((maxSizeRel.Width > 0f) && (maxSizeRel.Height > 0f))
			{
				SizeF optimalSize = this.GetOptimalSize(chartGraph, maxSizeRel);
				ef.Height = optimalSize.Height;
				ef.Width = optimalSize.Width;
				if (!float.IsNaN(optimalSize.Height) && !float.IsNaN(optimalSize.Width))
				{
					if (this.Docking == Docking.Top)
					{
						ef.Y = chartAreasRectangle.Y + elementSpacing;
						if (this.Alignment == StringAlignment.Near)
						{
							ef.X = chartAreasRectangle.X + elementSpacing;
						}
						else if (this.Alignment == StringAlignment.Far)
						{
							ef.X = (chartAreasRectangle.Right - optimalSize.Width) - elementSpacing;
						}
						else if (this.Alignment == StringAlignment.Center)
						{
							ef.X = chartAreasRectangle.X + ((chartAreasRectangle.Width - optimalSize.Width) / 2f);
						}
						chartAreasRectangle.Height -= ef.Height + elementSpacing;
						chartAreasRectangle.Y = ef.Bottom;
					}
					else if (this.Docking == Docking.Bottom)
					{
						ef.Y = (chartAreasRectangle.Bottom - optimalSize.Height) - elementSpacing;
						if (this.Alignment == StringAlignment.Near)
						{
							ef.X = chartAreasRectangle.X + elementSpacing;
						}
						else if (this.Alignment == StringAlignment.Far)
						{
							ef.X = (chartAreasRectangle.Right - optimalSize.Width) - elementSpacing;
						}
						else if (this.Alignment == StringAlignment.Center)
						{
							ef.X = chartAreasRectangle.X + ((chartAreasRectangle.Width - optimalSize.Width) / 2f);
						}
						chartAreasRectangle.Height -= ef.Height + elementSpacing;
					}
					if (this.Docking == Docking.Left)
					{
						ef.X = chartAreasRectangle.X + elementSpacing;
						if (this.Alignment == StringAlignment.Near)
						{
							ef.Y = chartAreasRectangle.Y + elementSpacing;
						}
						else if (this.Alignment == StringAlignment.Far)
						{
							ef.Y = (chartAreasRectangle.Bottom - optimalSize.Height) - elementSpacing;
						}
						else if (this.Alignment == StringAlignment.Center)
						{
							ef.Y = chartAreasRectangle.Y + ((chartAreasRectangle.Height - optimalSize.Height) / 2f);
						}
						chartAreasRectangle.Width -= ef.Width + elementSpacing;
						chartAreasRectangle.X = ef.Right;
					}
					if (this.Docking == Docking.Right)
					{
						ef.X = (chartAreasRectangle.Right - optimalSize.Width) - elementSpacing;
						if (this.Alignment == StringAlignment.Near)
						{
							ef.Y = chartAreasRectangle.Y + elementSpacing;
						}
						else if (this.Alignment == StringAlignment.Far)
						{
							ef.Y = (chartAreasRectangle.Bottom - optimalSize.Height) - elementSpacing;
						}
						else if (this.Alignment == StringAlignment.Center)
						{
							ef.Y = chartAreasRectangle.Y + ((chartAreasRectangle.Height - optimalSize.Height) / 2f);
						}
						chartAreasRectangle.Width -= ef.Width + elementSpacing;
					}
					this.Position.SetPositionNoAuto(ef.X, ef.Y, ef.Width, ef.Height);
				}
			}
		}

		private bool CheckLegendItemsFit(ChartGraphics graph, Size legendItemsAreaSize, int numberOfItemsToCheck, int fontSizeReducedBy, int numberOfColumns, int[] numberOfRowsPerColumn, out int[,] subColumnSizes, out int[,] cellHeights, out int horizontalSpaceLeft, out int verticalSpaceLeft)
		{
			bool flag = true;
			horizontalSpaceLeft = 0;
			verticalSpaceLeft = 0;
			if (numberOfItemsToCheck < 0)
			{
				numberOfItemsToCheck = this.legendItems.Count;
			}
			int numberOfCells = this.GetNumberOfCells();
			int maximumNumberOfRows = this.GetMaximumNumberOfRows(numberOfRowsPerColumn);
			int[, ,] numArray = new int[numberOfColumns, maximumNumberOfRows, numberOfCells];
			cellHeights = new int[numberOfColumns, maximumNumberOfRows];
			this.singleWCharacterSize = graph.MeasureStringAbs("W", (this.autofitFont == null) ? this.Font : this.autofitFont);
			Size size = graph.MeasureStringAbs("WW", (this.autofitFont == null) ? this.Font : this.autofitFont);
			this.singleWCharacterSize.Width = size.Width - this.singleWCharacterSize.Width;
			int index = 0;
			int num4 = 0;
			for (int i = 0; i < numberOfItemsToCheck; i++)
			{
				LegendItem item = this.legendItems[i];
				int num6 = 0;
				for (int j = 0; j < item.Cells.Count; j++)
				{
					LegendCell cell = item.Cells[j];
					LegendCellColumn column = null;
					if (j < this.CellColumns.Count)
					{
						column = this.CellColumns[j];
					}
					if (num6 > 0)
					{
						numArray[index, num4, j] = -1;
						num6--;
					}
					else
					{
						if (cell.CellSpan > 1)
						{
							num6 = cell.CellSpan - 1;
						}
						Size size2 = cell.MeasureCell(graph, fontSizeReducedBy, (this.autofitFont == null) ? this.Font : this.autofitFont, this.singleWCharacterSize);
						if (column != null)
						{
							if (column.MinimumWidth >= 0)
							{
								size2.Width = (int)Math.Max((float)size2.Width, ((float)(column.MinimumWidth * this.singleWCharacterSize.Width)) / 100f);
							}
							if (column.MaximumWidth >= 0)
							{
								size2.Width = (int)Math.Min((float)size2.Width, ((float)(column.MaximumWidth * this.singleWCharacterSize.Width)) / 100f);
							}
						}
						numArray[index, num4, j] = size2.Width;
						if (j == 0)
						{
							cellHeights[index, num4] = size2.Height;
						}
						else
						{
							cellHeights[index, num4] = Math.Max(cellHeights[index, num4], size2.Height);
						}
					}
				}
				num4++;
				if (num4 >= numberOfRowsPerColumn[index])
				{
					index++;
					num4 = 0;
					if (index >= numberOfColumns)
					{
						if (i < (numberOfItemsToCheck - 1))
						{
							flag = false;
						}
						break;
					}
				}
			}
			subColumnSizes = new int[numberOfColumns, numberOfCells];
			bool flag2 = false;
			for (index = 0; index < numberOfColumns; index++)
			{
				for (int k = 0; k < numberOfCells; k++)
				{
					int num9 = 0;
					num4 = 0;
					while (num4 < numberOfRowsPerColumn[index])
					{
						int num10 = numArray[index, num4, k];
						if (num10 < 0)
						{
							flag2 = true;
						}
						else
						{
							if ((k + 1) < numberOfCells)
							{
								int num11 = numArray[index, num4, k + 1];
								if (num11 < 0)
								{
									goto Label_02D0;
								}
							}
							num9 = Math.Max(num9, num10);
						}
					Label_02D0:
						num4++;
					}
					subColumnSizes[index, k] = num9;
				}
			}
			for (index = 0; index < numberOfColumns; index++)
			{
				for (int m = 0; m < numberOfCells; m++)
				{
					if (m < this.CellColumns.Count)
					{
						LegendCellColumn column2 = this.CellColumns[m];
						if (column2.HeaderText.Length > 0)
						{
							Size size3 = graph.MeasureStringAbs(column2.HeaderText + "I", column2.HeaderFont);
							if (size3.Width > subColumnSizes[index, m])
							{
								subColumnSizes[index, m] = size3.Width;
								if (column2.MinimumWidth >= 0)
								{
									subColumnSizes[index, m] = (int)Math.Max((float)subColumnSizes[index, m], ((float)(column2.MinimumWidth * this.singleWCharacterSize.Width)) / 100f);
								}
								if (column2.MaximumWidth >= 0)
								{
									subColumnSizes[index, m] = (int)Math.Min((float)subColumnSizes[index, m], ((float)(column2.MaximumWidth * this.singleWCharacterSize.Width)) / 100f);
								}
							}
						}
					}
				}
			}
			if (flag2)
			{
				index = 0;
				while (index < numberOfColumns)
				{
					for (int n = 0; n < numberOfCells; n++)
					{
						num4 = 0;
						while (num4 < numberOfRowsPerColumn[index])
						{
							int num14 = numArray[index, num4, n];
							int num15 = 0;
							while (((n + num15) + 1) < numberOfCells)
							{
								int num16 = numArray[index, num4, (n + num15) + 1];
								if (num16 >= 0)
								{
									break;
								}
								num15++;
							}
							if (num15 > 0)
							{
								int num17 = 0;
								for (int num18 = 0; num18 <= num15; num18++)
								{
									num17 += subColumnSizes[index, n + num18];
								}
								if (num14 > num17)
								{
									int num1 = subColumnSizes[index, n + num15];
									num1 += num14 - num17;//num1[0] += num14 - num17;
								}
							}
							num4++;
						}
					}
					index++;
				}
			}
			if (this.IsEquallySpacedItems)
			{
				for (int num19 = 0; num19 < numberOfCells; num19++)
				{
					int num20 = 0;
					index = 0;
					while (index < numberOfColumns)
					{
						num20 = Math.Max(num20, subColumnSizes[index, num19]);
						index++;
					}
					index = 0;
					while (index < numberOfColumns)
					{
						subColumnSizes[index, num19] = num20;
						index++;
					}
				}
			}
			int num21 = 0;
			int num22 = 0;
			for (index = 0; index < numberOfColumns; index++)
			{
				for (int num23 = 0; num23 < numberOfCells; num23++)
				{
					num21 += subColumnSizes[index, num23];
				}
				if (index < (numberOfColumns - 1))
				{
					num22 += this.GetSeparatorSize(this.ItemColumnSeparator).Width;
				}
			}
			int num24 = 0;
			for (index = 0; index < numberOfColumns; index++)
			{
				int num25 = 0;
				for (num4 = 0; num4 < numberOfRowsPerColumn[index]; num4++)
				{
					num25 += cellHeights[index, num4];
				}
				num24 = Math.Max(num24, num25);
			}
			horizontalSpaceLeft = (legendItemsAreaSize.Width - num21) - num22;
			if (horizontalSpaceLeft < 0)
			{
				flag = false;
			}
			verticalSpaceLeft = legendItemsAreaSize.Height - num24;
			if (verticalSpaceLeft < 0)
			{
				flag = false;
			}
			return flag;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (this._fontCache != null)
				{
					this._fontCache.Dispose();
					this._fontCache = null;
				}
				if (this.legendItems != null)
				{
					this.legendItems.Dispose();
					this.legendItems = null;
				}
				if (this._cellColumns != null)
				{
					this._cellColumns.Dispose();
					this._cellColumns = null;
				}
				if (this._customLegends != null)
				{
					this._customLegends.Dispose();
					this._customLegends = null;
				}
				if (this._position != null)
				{
					this._position.Dispose();
					this._position = null;
				}
			}
		}

		private void DrawLegendHeader(ChartGraphics chartGraph)
		{
			if ((!this._headerPosition.IsEmpty && (this._headerPosition.Width > 0)) && (this._headerPosition.Height > 0))
			{
				int right = -1;
				bool flag = false;
				Rectangle rect = Rectangle.Round(chartGraph.GetAbsoluteRectangle(this.Position.ToRectangleF()));
				rect.Y += this.GetBorderSize();
				rect.Height -= 2 * (this._offset.Height + this.GetBorderSize());
				rect.X += this.GetBorderSize();
				rect.Width -= 2 * this.GetBorderSize();
				if (this.GetBorderSize() > 0)
				{
					rect.Height++;
					rect.Width++;
				}
				bool flag2 = false;
				for (int i = 0; i < this.CellColumns.Count; i++)
				{
					LegendCellColumn column = this.CellColumns[i];
					if (!column.HeaderBackColor.IsEmpty)
					{
						flag2 = true;
					}
				}
				for (int j = 0; j < this._itemColumns; j++)
				{
					int left = 0;
					int num5 = 0;
					int length = this._subColumnSizes.GetLength(1);
					for (int k = 0; k < length; k++)
					{
						Rectangle rectangle = this._headerPosition;
						if (this._horizontalSpaceLeft > 0)
						{
							rectangle.X += (int)(((float)this._horizontalSpaceLeft) / 2f);
						}
						if (right != -1)
						{
							rectangle.X = right;
						}
						rectangle.Width = this._subColumnSizes[j, k];
						right = rectangle.Right;
						if (k == 0)
						{
							left = rectangle.Left;
						}
						num5 += rectangle.Width;
						rectangle.Intersect(rect);
						if ((rectangle.Width > 0) && (rectangle.Height > 0))
						{
							Rectangle rectangle3 = rectangle;
							if (this._titlePosition.Height <= 0)
							{
								rectangle3.Y -= this._offset.Height;
								rectangle3.Height += this._offset.Height;
							}
							if (((this._itemColumns == 1) && flag2) || (this.ItemColumnSeparator != LegendSeparatorStyle.None))
							{
								if ((j == 0) && (k == 0))
								{
									int x = rect.X;
									num5 += left - x;
									left = x;
									rectangle3.Width += rectangle3.X - rect.X;
									rectangle3.X = x;
								}
								if ((j == (this._itemColumns - 1)) && (k == (length - 1)))
								{
									num5 += (rect.Right - rectangle3.Right) + 1;
									rectangle3.Width += (rect.Right - rectangle3.Right) + 1;
								}
								if ((j != 0) && (k == 0))
								{
									num5 += this._itemColumnSpacingRel / 2;
									left -= this._itemColumnSpacingRel / 2;
									rectangle3.Width += this._itemColumnSpacingRel / 2;
									rectangle3.X -= this._itemColumnSpacingRel / 2;
								}
								if ((j != (this._itemColumns - 1)) && (k == (length - 1)))
								{
									num5 += this._itemColumnSpacingRel / 2;
									rectangle3.Width += this._itemColumnSpacingRel / 2;
								}
							}
							if (k < this.CellColumns.Count)
							{
								LegendCellColumn column2 = this.CellColumns[k];
								if (!column2.HeaderBackColor.IsEmpty)
								{
									flag = true;
									if (rectangle3.Right > rect.Right)
									{
										rectangle3.Width -= rect.Right - rectangle3.Right;
									}
									if (rectangle3.X < rect.X)
									{
										rectangle3.X += rect.X - rectangle3.X;
										rectangle3.Width -= rect.X - rectangle3.X;
									}
									rectangle3.Intersect(rect);
									chartGraph.FillRectangleRel(chartGraph.GetRelativeRectangle(rectangle3), column2.HeaderBackColor, ChartHatchStyle.None, string.Empty, ChartImageWrapMode.Tile, Color.Empty, ChartImageAlignmentStyle.Center, GradientStyle.None, Color.Empty, Color.Empty, 0, ChartDashStyle.NotSet, Color.Empty, 0, PenAlignment.Inset);
								}
								using (SolidBrush brush = new SolidBrush(column2.HeaderForeColor))
								{
									using (StringFormat format = new StringFormat())
									{
										format.Alignment = column2.HeaderAlignment;
										format.LineAlignment = StringAlignment.Center;
										format.FormatFlags = StringFormatFlags.LineLimit;
										format.Trimming = StringTrimming.EllipsisCharacter;
										chartGraph.DrawStringRel(column2.HeaderText, column2.HeaderFont, brush, chartGraph.GetRelativeRectangle(rectangle), format);
									}
								}
							}
						}
					}
					Rectangle position = this._headerPosition;
					position.X = left;
					position.Width = num5;
					if ((this.HeaderSeparator == LegendSeparatorStyle.Line) || (this.HeaderSeparator == LegendSeparatorStyle.DoubleLine))
					{
						rect.Width--;
					}
					position.Intersect(rect);
					this.DrawSeparator(chartGraph, this.HeaderSeparator, this.HeaderSeparatorColor, true, position);
					right += this.GetSeparatorSize(this.ItemColumnSeparator).Width;
				}
				if (flag)
				{
					chartGraph.FillRectangleRel(chartGraph.GetRelativeRectangle(Rectangle.Round(chartGraph.GetAbsoluteRectangle(this.Position.ToRectangleF()))), Color.Transparent, ChartHatchStyle.None, string.Empty, ChartImageWrapMode.Tile, Color.Empty, ChartImageAlignmentStyle.Center, GradientStyle.None, Color.Empty, this.BorderColor, this.GetBorderSize(), this.BorderDashStyle, Color.Empty, 0, PenAlignment.Inset);
				}
				if (base.Common.ProcessModeRegions && !this._headerPosition.IsEmpty)
				{
					base.Common.HotRegionsList.AddHotRegion(chartGraph.GetRelativeRectangle(this._headerPosition), this, ChartElementType.LegendHeader, true);
				}
			}
		}

		private void DrawLegendTitle(ChartGraphics chartGraph)
		{
			if ((this.Title.Length > 0) && !this._titlePosition.IsEmpty)
			{
				Rectangle rect = Rectangle.Round(chartGraph.GetAbsoluteRectangle(this.Position.ToRectangleF()));
				rect.Y += this.GetBorderSize();
				rect.Height -= 2 * this.GetBorderSize();
				rect.X += this.GetBorderSize();
				rect.Width -= 2 * this.GetBorderSize();
				if (this.GetBorderSize() > 0)
				{
					rect.Height++;
					rect.Width++;
				}
				if (!this.TitleBackColor.IsEmpty)
				{
					Rectangle rectangle = this._titlePosition;
					rectangle.Intersect(rect);
					chartGraph.FillRectangleRel(chartGraph.GetRelativeRectangle(rectangle), this.TitleBackColor, ChartHatchStyle.None, string.Empty, ChartImageWrapMode.Tile, Color.Empty, ChartImageAlignmentStyle.Center, GradientStyle.None, Color.Empty, Color.Empty, 0, ChartDashStyle.NotSet, Color.Empty, 0, PenAlignment.Inset);
				}
				using (SolidBrush brush = new SolidBrush(this.TitleForeColor))
				{
					StringFormat format = new StringFormat();
					format.Alignment = this.TitleAlignment;
					Rectangle rectangle3 = this._titlePosition;
					rectangle3.Y += this._offset.Height;
					rectangle3.X += this._offset.Width;
					rectangle3.X += this.GetBorderSize();
					rectangle3.Width -= (this.GetBorderSize() * 2) + this._offset.Width;
					rectangle3.Intersect(rect);
					chartGraph.DrawStringRel(this.Title.Replace(@"\n", "\n"), this.TitleFont, brush, chartGraph.GetRelativeRectangle(rectangle3), format);
				}
				Rectangle position = this._titlePosition;
				if ((this.TitleSeparator == LegendSeparatorStyle.Line) || (this.TitleSeparator == LegendSeparatorStyle.DoubleLine))
				{
					rect.Width--;
				}
				position.Intersect(rect);
				this.DrawSeparator(chartGraph, this.TitleSeparator, this.TitleSeparatorColor, true, position);
				if (!this.TitleBackColor.IsEmpty || (this.TitleSeparator != LegendSeparatorStyle.None))
				{
					chartGraph.FillRectangleRel(chartGraph.GetRelativeRectangle(Rectangle.Round(chartGraph.GetAbsoluteRectangle(this.Position.ToRectangleF()))), Color.Transparent, ChartHatchStyle.None, string.Empty, ChartImageWrapMode.Tile, Color.Empty, ChartImageAlignmentStyle.Center, GradientStyle.None, Color.Empty, this.BorderColor, this.GetBorderSize(), this.BorderDashStyle, Color.Empty, 0, PenAlignment.Inset);
				}
			}
		}

		private void DrawSeparator(ChartGraphics chartGraph, LegendSeparatorStyle separatorType, Color color, bool horizontal, Rectangle position)
		{
			SmoothingMode smoothingMode = chartGraph.SmoothingMode;
			chartGraph.SmoothingMode = SmoothingMode.None;
			RectangleF ef = position;
			if (!horizontal)
			{
				ef.X += (int)(((float)this._itemColumnSpacingRel) / 2f);
				ef.Width -= this._itemColumnSpacingRel;
			}
			if (separatorType == LegendSeparatorStyle.Line)
			{
				if (horizontal)
				{
					chartGraph.DrawLineAbs(color, 1, ChartDashStyle.Solid, new PointF(ef.Left, ef.Bottom - 1f), new PointF(ef.Right, ef.Bottom - 1f));
				}
				else
				{
					chartGraph.DrawLineAbs(color, 1, ChartDashStyle.Solid, new PointF(ef.Right - 1f, ef.Top), new PointF(ef.Right - 1f, ef.Bottom));
				}
			}
			else if (separatorType == LegendSeparatorStyle.DashLine)
			{
				if (horizontal)
				{
					chartGraph.DrawLineAbs(color, 1, ChartDashStyle.Dash, new PointF(ef.Left, ef.Bottom - 1f), new PointF(ef.Right, ef.Bottom - 1f));
				}
				else
				{
					chartGraph.DrawLineAbs(color, 1, ChartDashStyle.Dash, new PointF(ef.Right - 1f, ef.Top), new PointF(ef.Right - 1f, ef.Bottom));
				}
			}
			else if (separatorType == LegendSeparatorStyle.DotLine)
			{
				if (horizontal)
				{
					chartGraph.DrawLineAbs(color, 1, ChartDashStyle.Dot, new PointF(ef.Left, ef.Bottom - 1f), new PointF(ef.Right, ef.Bottom - 1f));
				}
				else
				{
					chartGraph.DrawLineAbs(color, 1, ChartDashStyle.Dot, new PointF(ef.Right - 1f, ef.Top), new PointF(ef.Right - 1f, ef.Bottom));
				}
			}
			else if (separatorType == LegendSeparatorStyle.ThickLine)
			{
				if (horizontal)
				{
					chartGraph.DrawLineAbs(color, 2, ChartDashStyle.Solid, new PointF(ef.Left, ef.Bottom - 1f), new PointF(ef.Right, ef.Bottom - 1f));
				}
				else
				{
					chartGraph.DrawLineAbs(color, 2, ChartDashStyle.Solid, new PointF(ef.Right - 1f, ef.Top), new PointF(ef.Right - 1f, ef.Bottom));
				}
			}
			else if (separatorType == LegendSeparatorStyle.DoubleLine)
			{
				if (horizontal)
				{
					chartGraph.DrawLineAbs(color, 1, ChartDashStyle.Solid, new PointF(ef.Left, ef.Bottom - 3f), new PointF(ef.Right, ef.Bottom - 3f));
					chartGraph.DrawLineAbs(color, 1, ChartDashStyle.Solid, new PointF(ef.Left, ef.Bottom - 1f), new PointF(ef.Right, ef.Bottom - 1f));
				}
				else
				{
					chartGraph.DrawLineAbs(color, 1, ChartDashStyle.Solid, new PointF(ef.Right - 3f, ef.Top), new PointF(ef.Right - 3f, ef.Bottom));
					chartGraph.DrawLineAbs(color, 1, ChartDashStyle.Solid, new PointF(ef.Right - 1f, ef.Top), new PointF(ef.Right - 1f, ef.Bottom));
				}
			}
			else if (separatorType == LegendSeparatorStyle.GradientLine)
			{
				if (horizontal)
				{
					chartGraph.FillRectangleAbs(new RectangleF(ef.Left, ef.Bottom - 1f, ef.Width, 0f), Color.Transparent, ChartHatchStyle.None, string.Empty, ChartImageWrapMode.Tile, Color.Empty, ChartImageAlignmentStyle.Center, GradientStyle.VerticalCenter, color, Color.Empty, 0, ChartDashStyle.NotSet, PenAlignment.Inset);
				}
				else
				{
					chartGraph.FillRectangleAbs(new RectangleF(ef.Right - 1f, ef.Top, 0f, ef.Height), Color.Transparent, ChartHatchStyle.None, string.Empty, ChartImageWrapMode.Tile, Color.Empty, ChartImageAlignmentStyle.Center, GradientStyle.HorizontalCenter, color, Color.Empty, 0, ChartDashStyle.NotSet, PenAlignment.Inset);
				}
			}
			else if (separatorType == LegendSeparatorStyle.ThickGradientLine)
			{
				if (horizontal)
				{
					chartGraph.FillRectangleAbs(new RectangleF(ef.Left, ef.Bottom - 2f, ef.Width, 1f), Color.Transparent, ChartHatchStyle.None, string.Empty, ChartImageWrapMode.Tile, Color.Empty, ChartImageAlignmentStyle.Center, GradientStyle.VerticalCenter, color, Color.Empty, 0, ChartDashStyle.NotSet, PenAlignment.Inset);
				}
				else
				{
					chartGraph.FillRectangleAbs(new RectangleF(ef.Right - 2f, ef.Top, 1f, ef.Height), Color.Transparent, ChartHatchStyle.None, string.Empty, ChartImageWrapMode.Tile, Color.Empty, ChartImageAlignmentStyle.Center, GradientStyle.HorizontalCenter, color, Color.Empty, 0, ChartDashStyle.NotSet, PenAlignment.Inset);
				}
			}
			chartGraph.SmoothingMode = smoothingMode;
		}

		private void FillLegendItemsCollection()
		{
			this.legendItems.Clear();
			foreach (Series series in base.Common.DataManager.Series)
			{
				if (base.Common.ChartPicture.Legends.IndexOf(series.Legend) < 0)
				{
					throw new InvalidOperationException(SR.ExceptionLegendReferencedInSeriesNotFound(series.Name, series.Legend));
				}
			}
			bool flag = false;
			foreach (Series series2 in base.Common.DataManager.Series)
			{
				if ((base.Common.ChartPicture.Legends[series2.Legend] != this) || (series2.ChartArea.Length <= 0))
				{
					continue;
				}
				bool flag2 = false;
				foreach (ChartArea area in base.Common.ChartPicture.ChartAreas)
				{
					if (area.Name == series2.ChartArea)
					{
						flag2 = true;
						break;
					}
				}
				if (series2.IsVisible() && flag2)
				{
					IChartType chartType = base.Common.ChartTypeRegistry.GetChartType(series2.ChartTypeName);
					if ((this.LegendItemOrder == LegendItemOrder.Auto) && (((series2.ChartType == SeriesChartType.StackedArea) || (series2.ChartType == SeriesChartType.StackedArea100)) || (((series2.ChartType == SeriesChartType.Pyramid) || (series2.ChartType == SeriesChartType.StackedColumn)) || (series2.ChartType == SeriesChartType.StackedColumn100))))
					{
						flag = true;
					}
					if (chartType.DataPointsInLegend)
					{
						bool flag3 = false;
						foreach (DataPoint point in series2.Points)
						{
							if (point.XValue != 0.0)
							{
								flag3 = true;
								break;
							}
						}
						int num = 0;
						foreach (DataPoint point2 in series2.Points)
						{
							if (point2.IsEmpty)
							{
								num++;
							}
							else
							{
								if (!point2.IsVisibleInLegend)
								{
									num++;
									continue;
								}
								LegendItem item = new LegendItem(point2.Label, point2.Color, "");
								bool flag4 = base.Common.Chart.ChartAreas[series2.ChartArea].Area3DStyle.Enable3D;
								item.SetAttributes(base.Common, series2);
								item.SetAttributes(point2, flag4);
								item.ToolTip = point2.ReplaceKeywords(point2.LegendToolTip);
								item.MapAreaAttributes = point2.ReplaceKeywords(point2.LegendMapAreaAttributes);
								item.PostBackValue = point2.ReplaceKeywords(point2.LegendPostBackValue);
								item.Url = point2.ReplaceKeywords(point2.LegendUrl);
								item.Name = point2.ReplaceKeywords(point2.LegendText);
								item.SeriesPointIndex = num++;
								if (item.Name.Length == 0)
								{
									item.Name = point2.ReplaceKeywords((point2.Label.Length > 0) ? point2.Label : point2.AxisLabel);
								}
								if ((item.Name.Length == 0) && flag3)
								{
									item.Name = ValueConverter.FormatValue(series2.Chart, this, base.Tag, point2.XValue, "", point2.series.XValueType, ChartElementType.LegendItem);
								}
								if (item.Name.Length == 0)
								{
									item.Name = "Point " + num;
								}
								item.AddAutomaticCells(this);
								foreach (LegendCell cell in item.Cells)
								{
									if (cell.Text.Length > 0)
									{
										cell.Text = cell.Text.Replace("#LEGENDTEXT", item.Name);
										cell.Text = point2.ReplaceKeywords(cell.Text);
										cell.ToolTip = point2.ReplaceKeywords(cell.ToolTip);
										cell.Url = point2.ReplaceKeywords(cell.Url);
										cell.MapAreaAttributes = point2.ReplaceKeywords(cell.MapAreaAttributes);
										cell.PostBackValue = point2.ReplaceKeywords(cell.PostBackValue);
									}
								}
								this.legendItems.Add(item);
							}
						}
						continue;
					}
					if (series2.IsVisibleInLegend)
					{
						LegendItem item2 = new LegendItem(series2.Name, series2.Color, "");
						item2.SetAttributes(base.Common, series2);
						item2.ToolTip = series2.ReplaceKeywords(series2.LegendToolTip);
						item2.Url = series2.ReplaceKeywords(series2.LegendUrl);
						item2.MapAreaAttributes = series2.ReplaceKeywords(series2.LegendMapAreaAttributes);
						item2.PostBackValue = series2.ReplaceKeywords(series2.LegendPostBackValue);
						if (series2.LegendText.Length > 0)
						{
							item2.Name = series2.ReplaceKeywords(series2.LegendText);
						}
						item2.AddAutomaticCells(this);
						foreach (LegendCell cell2 in item2.Cells)
						{
							if (cell2.Text.Length > 0)
							{
								cell2.Text = cell2.Text.Replace("#LEGENDTEXT", item2.Name);
								cell2.Text = series2.ReplaceKeywords(cell2.Text);
								cell2.ToolTip = series2.ReplaceKeywords(cell2.ToolTip);
								cell2.Url = series2.ReplaceKeywords(cell2.Url);
								cell2.MapAreaAttributes = series2.ReplaceKeywords(cell2.MapAreaAttributes);
								cell2.PostBackValue = series2.ReplaceKeywords(cell2.PostBackValue);
							}
						}
						this.legendItems.Add(item2);
					}
				}
			}
			if ((this.LegendItemOrder == LegendItemOrder.SameAsSeriesOrder) || ((this.LegendItemOrder == LegendItemOrder.Auto) && flag))
			{
				this.legendItems.Reverse();
			}
			foreach (LegendItem item3 in this._customLegends)
			{
				if (item3.Enabled)
				{
					this.legendItems.Add(item3);
				}
			}
			if (((this.legendItems.Count == 0) && (base.Common != null)) && ((base.Common.Chart != null) && base.Common.Chart.IsDesignMode()))
			{
				LegendItem item4 = new LegendItem(this.Name + " - " + SR.DescriptionTypeEmpty, Color.White, "");
				item4.ImageStyle = LegendImageStyle.Line;
				this.legendItems.Add(item4);
			}
			foreach (LegendItem item5 in this.legendItems)
			{
				item5.AddAutomaticCells(this);
			}
		}

		private int GetBorderSize()
		{
			if (((this.BorderWidth > 0) && (this.BorderDashStyle != ChartDashStyle.NotSet)) && (!this.BorderColor.IsEmpty && (this.BorderColor != Color.Transparent)))
			{
				return this.BorderWidth;
			}
			return 0;
		}

		private Rectangle GetCellPosition(int columnIndex, int rowIndex, int cellIndex, Size itemHalfSpacing)
		{
			Rectangle rectangle = this._legendItemsAreaPosition;
			for (int i = 0; i < rowIndex; i++)
			{
				rectangle.Y += this._cellHeights[columnIndex, i];
			}
			if (itemHalfSpacing.Height > 0)
			{
				rectangle.Y += ((itemHalfSpacing.Height * rowIndex) * 2) + itemHalfSpacing.Height;
			}
			if (this._horizontalSpaceLeft > 0)
			{
				rectangle.X += itemHalfSpacing.Width;
			}
			int numberOfCells = this.GetNumberOfCells();
			for (int j = 0; j < columnIndex; j++)
			{
				for (int m = 0; m < numberOfCells; m++)
				{
					rectangle.X += this._subColumnSizes[j, m];
				}
				rectangle.X += this.GetSeparatorSize(this.ItemColumnSeparator).Width;
			}
			for (int k = 0; k < cellIndex; k++)
			{
				rectangle.X += this._subColumnSizes[columnIndex, k];
			}
			rectangle.Height = this._cellHeights[columnIndex, rowIndex];
			rectangle.Width = this._subColumnSizes[columnIndex, cellIndex];
			return rectangle;
		}

		private int GetColumnHeight(int columnIndex)
		{
			int num = 0;
			for (int i = 0; i < this._numberOfRowsPerColumn[columnIndex]; i++)
			{
				num += this._cellHeights[columnIndex, i];
			}
			return num;
		}

		private Size GetHeaderSize(ChartGraphics chartGraph, LegendCellColumn legendColumn)
		{
			Size empty = Size.Empty;
			if (legendColumn.HeaderText.Length > 0)
			{
				empty = chartGraph.MeasureStringAbs(legendColumn.HeaderText.Replace(@"\n", "\n") + "I", legendColumn.HeaderFont);
				empty.Height += this._offset.Height;
				empty.Width += this._offset.Width;
				empty.Height += this.GetSeparatorSize(this.HeaderSeparator).Height;
			}
			return empty;
		}

		private LegendTableStyle GetLegendTableStyle(ChartGraphics chartGraph)
		{
			LegendTableStyle tableStyle = this.TableStyle;
			if (this.TableStyle != LegendTableStyle.Auto)
			{
				return tableStyle;
			}
			if (this.Position.Auto)
			{
				if ((this.Docking != Docking.Left) && (this.Docking != Docking.Right))
				{
					return LegendTableStyle.Wide;
				}
				return LegendTableStyle.Tall;
			}
			SizeF size = chartGraph.GetAbsoluteRectangle(this.Position.ToRectangleF()).Size;
			if (size.Width < size.Height)
			{
				return LegendTableStyle.Tall;
			}
			return LegendTableStyle.Wide;
		}

		private int GetMaximumNumberOfRows()
		{
			return this.GetMaximumNumberOfRows(this._numberOfRowsPerColumn);
		}

		private int GetMaximumNumberOfRows(int[] rowsPerColumn)
		{
			int num = 0;
			if (rowsPerColumn != null)
			{
				for (int i = 0; i < rowsPerColumn.Length; i++)
				{
					num = Math.Max(num, rowsPerColumn[i]);
				}
			}
			return num;
		}

		private int GetNumberOfCells()
		{
			if (this._numberOfCells < 0)
			{
				this._numberOfCells = this.CellColumns.Count;
				foreach (LegendItem item in this.legendItems)
				{
					this._numberOfCells = Math.Max(this._numberOfCells, item.Cells.Count);
				}
			}
			return this._numberOfCells;
		}

		private void GetNumberOfRowsAndColumns(ChartGraphics chartGraph, Size legendSize, int numberOfItemsToCheck, out int[] numberOfRowsPerColumn, out int columnNumber)
		{
			int horSpaceLeft = 0;
			int vertSpaceLeft = 0;
			this.GetNumberOfRowsAndColumns(chartGraph, legendSize, numberOfItemsToCheck, out numberOfRowsPerColumn, out columnNumber, out horSpaceLeft, out vertSpaceLeft);
		}

		private void GetNumberOfRowsAndColumns(ChartGraphics chartGraph, Size legendSize, int numberOfItemsToCheck, out int[] numberOfRowsPerColumn, out int columnNumber, out int horSpaceLeft, out int vertSpaceLeft)
		{
			numberOfRowsPerColumn = null;
			columnNumber = 1;
			horSpaceLeft = 0;
			vertSpaceLeft = 0;
			if (numberOfItemsToCheck < 0)
			{
				numberOfItemsToCheck = this.legendItems.Count;
			}
			if ((this.LegendStyle == LegendStyle.Column) || (numberOfItemsToCheck <= 1))
			{
				columnNumber = 1;
				numberOfRowsPerColumn = new int[] { numberOfItemsToCheck };
			}
			else if (this.LegendStyle == LegendStyle.Row)
			{
				columnNumber = numberOfItemsToCheck;
				numberOfRowsPerColumn = new int[columnNumber];
				for (int i = 0; i < columnNumber; i++)
				{
					numberOfRowsPerColumn[i] = 1;
				}
			}
			else if (this.LegendStyle == LegendStyle.Table)
			{
				columnNumber = 1;
				numberOfRowsPerColumn = new int[] { 1 };
				switch (this.GetLegendTableStyle(chartGraph))
				{
					case LegendTableStyle.Tall:
						{
							bool flag = false;
							int num2 = 1;
							num2 = 1;
							while (!flag && (num2 < numberOfItemsToCheck))
							{
								numberOfRowsPerColumn[columnNumber - 1]++;
								if (!this.CheckLegendItemsFit(chartGraph, legendSize, num2 + 1, this._autoFitFontSizeAdjustment, columnNumber, numberOfRowsPerColumn, out this._subColumnSizes, out this._cellHeights, out horSpaceLeft, out vertSpaceLeft) && (((columnNumber != 1) && (horSpaceLeft >= 0)) || (vertSpaceLeft <= 0)))
								{
									if (numberOfRowsPerColumn[columnNumber - 1] > 1)
									{
										numberOfRowsPerColumn[columnNumber - 1]--;
									}
									int num3 = 0;
									if (horSpaceLeft > 0)
									{
										num3 = ((int)Math.Round((double)(((double)(legendSize.Width - horSpaceLeft)) / ((double)columnNumber)))) / 2;
									}
									if ((columnNumber < 50) && (horSpaceLeft >= num3))
									{
										columnNumber++;
										int[] numArray = numberOfRowsPerColumn;
										numberOfRowsPerColumn = new int[columnNumber];
										for (int j = 0; j < numArray.Length; j++)
										{
											numberOfRowsPerColumn[j] = numArray[j];
										}
										numberOfRowsPerColumn[columnNumber - 1] = 1;
										if (num2 == (numberOfItemsToCheck - 1))
										{
											this.CheckLegendItemsFit(chartGraph, legendSize, num2 + 1, this._autoFitFontSizeAdjustment, columnNumber, numberOfRowsPerColumn, out this._subColumnSizes, out this._cellHeights, out horSpaceLeft, out vertSpaceLeft);
										}
									}
									else
									{
										flag = true;
									}
								}
								num2++;
							}
							if (columnNumber > 1)
							{
								bool flag3 = false;
								while (!flag3)
								{
									flag3 = true;
									int num5 = -1;
									for (int k = 0; k < columnNumber; k++)
									{
										int num7 = 0;
										for (int n = 0; n < (this._numberOfRowsPerColumn[k] - 1); n++)
										{
											num7 += this._cellHeights[k, n];
										}
										num5 = Math.Max(num5, num7);
									}
									int num9 = 0;
									for (int m = 0; m < (columnNumber - 1); m++)
									{
										if (this._numberOfRowsPerColumn[m] > 1)
										{
											num9 += this._cellHeights[m, this._numberOfRowsPerColumn[m] - 1];
										}
									}
									if ((num9 > 0) && ((this.GetColumnHeight(columnNumber - 1) + num9) <= num5))
									{
										int num12 = 0;
										for (int num13 = 0; num13 < (columnNumber - 1); num13++)
										{
											if (this._numberOfRowsPerColumn[num13] > 1)
											{
												this._numberOfRowsPerColumn[num13]--;
												num12++;
											}
										}
										if (num12 > 0)
										{
											this._numberOfRowsPerColumn[columnNumber - 1] += num12;
											this.CheckLegendItemsFit(chartGraph, legendSize, num2 + 1, this._autoFitFontSizeAdjustment, columnNumber, numberOfRowsPerColumn, out this._subColumnSizes, out this._cellHeights, out horSpaceLeft, out vertSpaceLeft);
											flag3 = false;
										}
									}
								}
							}
							goto Label_05A9;
						}
					case LegendTableStyle.Wide:
						{
							bool flag4 = false;
							int num14 = 1;
							for (num14 = 1; !flag4 && (num14 < numberOfItemsToCheck); num14++)
							{
								columnNumber++;
								int[] numArray2 = numberOfRowsPerColumn;
								numberOfRowsPerColumn = new int[columnNumber];
								for (int num15 = 0; num15 < numArray2.Length; num15++)
								{
									numberOfRowsPerColumn[num15] = numArray2[num15];
								}
								numberOfRowsPerColumn[columnNumber - 1] = 1;
								bool flag5 = this.CheckLegendItemsFit(chartGraph, legendSize, num14 + 1, this._autoFitFontSizeAdjustment, columnNumber, numberOfRowsPerColumn, out this._subColumnSizes, out this._cellHeights, out horSpaceLeft, out vertSpaceLeft);
								if (!flag5 && (((this.GetMaximumNumberOfRows(numberOfRowsPerColumn) != 1) && (vertSpaceLeft >= 0)) || (horSpaceLeft <= 0)))
								{
									bool flag6 = true;
									while (flag6)
									{
										flag6 = false;
										int num16 = 0;
										if (columnNumber > 1)
										{
											num16 = numberOfRowsPerColumn[columnNumber - 1];
											columnNumber--;
											numArray2 = numberOfRowsPerColumn;
											numberOfRowsPerColumn = new int[columnNumber];
											for (int num17 = 0; num17 < columnNumber; num17++)
											{
												numberOfRowsPerColumn[num17] = numArray2[num17];
											}
										}
										for (int num18 = 0; num18 < num16; num18++)
										{
											int index = -1;
											int num20 = 0x7fffffff;
											for (int num21 = 0; num21 < columnNumber; num21++)
											{
												int columnHeight = this.GetColumnHeight(num21);
												int num23 = 0;
												if (num21 < (columnNumber - 1))
												{
													num23 = this._cellHeights[num21 + 1, 0];
												}
												if ((columnHeight < num20) && ((columnHeight + num23) < legendSize.Height))
												{
													num20 = columnHeight;
													index = num21;
												}
											}
											if (index < 0)
											{
												flag5 = this.CheckLegendItemsFit(chartGraph, legendSize, num14 + 1, this._autoFitFontSizeAdjustment, columnNumber, numberOfRowsPerColumn, out this._subColumnSizes, out this._cellHeights, out horSpaceLeft, out vertSpaceLeft);
												flag4 = true;
												break;
											}
											numberOfRowsPerColumn[index]++;
											if ((index < (columnNumber - 1)) && (numberOfRowsPerColumn[index + 1] == 1))
											{
												numArray2 = numberOfRowsPerColumn;
												for (int num24 = index + 1; num24 < (numArray2.Length - 1); num24++)
												{
													numberOfRowsPerColumn[num24] = numArray2[num24 + 1];
												}
												numberOfRowsPerColumn[columnNumber - 1] = 1;
											}
											flag5 = this.CheckLegendItemsFit(chartGraph, legendSize, num14 + 1, this._autoFitFontSizeAdjustment, columnNumber, numberOfRowsPerColumn, out this._subColumnSizes, out this._cellHeights, out horSpaceLeft, out vertSpaceLeft);
										}
										if ((!flag5 && (((float)horSpaceLeft) < 0f)) && (columnNumber > 1))
										{
											flag6 = true;
										}
									}
								}
							}
							goto Label_05A9;
						}
				}
			}
		Label_05A9:
			this.CheckLegendItemsFit(chartGraph, legendSize, -1, this._autoFitFontSizeAdjustment, columnNumber, numberOfRowsPerColumn, out this._subColumnSizes, out this._cellHeights, out horSpaceLeft, out vertSpaceLeft);
		}

		private SizeF GetOptimalSize(ChartGraphics chartGraph, SizeF maxSizeRel)
		{
			this._offset = Size.Empty;
			this._itemColumns = 0;
			this._horizontalSpaceLeft = 0;
			this._verticalSpaceLeft = 0;
			this._subColumnSizes = null;
			this._numberOfRowsPerColumn = null;
			this._cellHeights = null;
			this.autofitFont = null;
			this._autoFitFontSizeAdjustment = 0;
			this._numberOfCells = -1;
			this._numberOfLegendItemsToProcess = -1;
			Size empty = Size.Empty;
			SizeF absoluteSize = chartGraph.GetAbsoluteSize(maxSizeRel);
			Size titleMaxSize = new Size((int)absoluteSize.Width, (int)absoluteSize.Height);
			foreach (LegendItem item in this.legendItems)
			{
				foreach (LegendCell cell in item.Cells)
				{
					cell.ResetCache();
				}
			}
			if (this.IsEnabled())
			{
				this.FillLegendItemsCollection();
				base.Common.Chart.CallOnCustomizeLegend(this.legendItems, this.Name);
				if (this.legendItems.Count > 0)
				{
					this.singleWCharacterSize = chartGraph.MeasureStringAbs("W", this.Font);
					Size size3 = chartGraph.MeasureStringAbs("WW", this.Font);
					this.singleWCharacterSize.Width = size3.Width - this.singleWCharacterSize.Width;
					this._offset.Width = (int)Math.Ceiling((double)(((float)this.singleWCharacterSize.Width) / 2f));
					this._offset.Height = (int)Math.Ceiling((double)(((float)this.singleWCharacterSize.Width) / 3f));
					this._itemColumnSpacingRel = (int)(this.singleWCharacterSize.Width * (((float)this._itemColumnSpacing) / 100f));
					if ((this._itemColumnSpacingRel % 2) == 1)
					{
						this._itemColumnSpacingRel++;
					}
					Size titleSize = Size.Empty;
					if (this.Title.Length > 0)
					{
						titleSize = this.GetTitleSize(chartGraph, titleMaxSize);
					}
					Size size5 = Size.Empty;
					foreach (LegendCellColumn column in this.CellColumns)
					{
						if (column.HeaderText.Length > 0)
						{
							Size headerSize = this.GetHeaderSize(chartGraph, column);
							size5.Height = Math.Max(size5.Height, headerSize.Height);
						}
					}
					Size legendSize = titleMaxSize;
					legendSize.Width -= 2 * (this._offset.Width + this.GetBorderSize());
					legendSize.Height -= 2 * (this._offset.Height + this.GetBorderSize());
					legendSize.Height -= titleSize.Height;
					legendSize.Height -= size5.Height;
					this._autoFitFontSizeAdjustment = 0;
					this.autofitFont = this.Font;
					int vertSpaceLeft = 0;
					int horSpaceLeft = 0;
					bool isTextAutoFit = this.IsTextAutoFit;
					bool flag2 = false;
					do
					{
						this.GetNumberOfRowsAndColumns(chartGraph, legendSize, -1, out this._numberOfRowsPerColumn, out this._itemColumns, out horSpaceLeft, out vertSpaceLeft);
						int num3 = 0;
						for (int i = 0; i < this._itemColumns; i++)
						{
							num3 += this._numberOfRowsPerColumn[i];
						}
						flag2 = ((horSpaceLeft >= 0) && (vertSpaceLeft >= 0)) && (num3 >= this.legendItems.Count);
						if (isTextAutoFit && !flag2)
						{
							if ((this.Font.Size - this._autoFitFontSizeAdjustment) > this._autoFitMinFontSize)
							{
								this._autoFitFontSizeAdjustment++;
								int num5 = (int)Math.Round((double)(this.Font.Size - this._autoFitFontSizeAdjustment));
								if (num5 < 1)
								{
									num5 = 1;
								}
								this.autofitFont = base.Common.ChartPicture.FontCache.GetFont(this.Font.FontFamily, (float)num5, this.Font.Style, this.Font.Unit);
							}
							else
							{
								isTextAutoFit = false;
							}
						}
					}
					while (isTextAutoFit && !flag2);
					horSpaceLeft -= Math.Min(4, horSpaceLeft);
					vertSpaceLeft -= Math.Min(2, vertSpaceLeft);
					empty.Width = legendSize.Width - horSpaceLeft;
					empty.Width = Math.Max(empty.Width, titleSize.Width);
					empty.Width += 2 * (this._offset.Width + this.GetBorderSize());
					empty.Height = ((legendSize.Height - vertSpaceLeft) + titleSize.Height) + size5.Height;
					empty.Height += 2 * (this._offset.Height + this.GetBorderSize());
					if ((horSpaceLeft < 0) || (vertSpaceLeft < 0))
					{
						empty.Height += this._truncatedDotsSize;
					}
					if (empty.Width > titleMaxSize.Width)
					{
						empty.Width = titleMaxSize.Width;
					}
					if (empty.Height > titleMaxSize.Height)
					{
						empty.Height = titleMaxSize.Height;
					}
					if (empty.Width < 0)
					{
						empty.Width = 0;
					}
					if (empty.Height < 0)
					{
						empty.Height = 0;
					}
				}
			}
			return chartGraph.GetRelativeSize((SizeF)empty);
		}

		internal Size GetSeparatorSize(LegendSeparatorStyle separatorType)
		{
			Size empty = Size.Empty;
			if (separatorType == LegendSeparatorStyle.None)
			{
				empty = Size.Empty;
			}
			else if (separatorType == LegendSeparatorStyle.Line)
			{
				empty = new Size(1, 1);
			}
			else if (separatorType == LegendSeparatorStyle.DashLine)
			{
				empty = new Size(1, 1);
			}
			else if (separatorType == LegendSeparatorStyle.DotLine)
			{
				empty = new Size(1, 1);
			}
			else if (separatorType == LegendSeparatorStyle.ThickLine)
			{
				empty = new Size(2, 2);
			}
			else if (separatorType == LegendSeparatorStyle.DoubleLine)
			{
				empty = new Size(3, 3);
			}
			else if (separatorType == LegendSeparatorStyle.GradientLine)
			{
				empty = new Size(1, 1);
			}
			else
			{
				if (separatorType != LegendSeparatorStyle.ThickGradientLine)
				{
					throw new InvalidOperationException(SR.ExceptionLegendSeparatorTypeUnknown(separatorType.ToString()));
				}
				empty = new Size(2, 2);
			}
			empty.Width += this._itemColumnSpacingRel;
			return empty;
		}

		private Size GetTitleSize(ChartGraphics chartGraph, Size titleMaxSize)
		{
			Size empty = Size.Empty;
			if (this.Title.Length > 0)
			{
				titleMaxSize.Width -= (this.GetBorderSize() * 2) + this._offset.Width;
				empty = chartGraph.MeasureStringAbs(this.Title.Replace(@"\n", "\n"), this.TitleFont, (SizeF)titleMaxSize, StringFormat.GenericTypographic);
				empty.Height += this._offset.Height;
				empty.Width += this._offset.Width;
				empty.Height += this.GetSeparatorSize(this.TitleSeparator).Height;
			}
			return empty;
		}

		internal void Invalidate(bool invalidateLegendOnly)
		{
		}

		internal bool IsEnabled()
		{
			if (!this.Enabled)
			{
				return false;
			}
			if (((this.DockedToChartArea.Length > 0) && (base.Common != null)) && ((base.Common.ChartPicture != null) && (base.Common.ChartPicture.ChartAreas.IndexOf(this.DockedToChartArea) >= 0)))
			{
				ChartArea area = base.Common.ChartPicture.ChartAreas[this.DockedToChartArea];
				if (!area.Visible)
				{
					return false;
				}
			}
			return true;
		}

		internal void Paint(ChartGraphics chartGraph)
		{
			this._offset = Size.Empty;
			this._itemColumns = 0;
			this._horizontalSpaceLeft = 0;
			this._verticalSpaceLeft = 0;
			this._subColumnSizes = null;
			this._numberOfRowsPerColumn = null;
			this._cellHeights = null;
			this.autofitFont = null;
			this._autoFitFontSizeAdjustment = 0;
			this._numberOfCells = -1;
			this._numberOfLegendItemsToProcess = -1;
			if (this.IsEnabled() && ((this.MaximumAutoSize != 0f) || !this.Position.Auto))
			{
				this.FillLegendItemsCollection();
				foreach (LegendItem item in this.legendItems)
				{
					foreach (LegendCell cell in item.Cells)
					{
						cell.ResetCache();
					}
				}
				base.Common.Chart.CallOnCustomizeLegend(this.legendItems, this.Name);
				if (this.legendItems.Count != 0)
				{
					this.RecalcLegendInfo(chartGraph);
					if (base.Common.ProcessModePaint)
					{
						chartGraph.FillRectangleRel(chartGraph.GetRelativeRectangle(Rectangle.Round(chartGraph.GetAbsoluteRectangle(this.Position.ToRectangleF()))), this.BackColor, this.BackHatchStyle, this.BackImage, this.BackImageWrapMode, this.BackImageTransparentColor, this.BackImageAlignment, this.BackGradientStyle, this.BackSecondaryColor, this.BorderColor, this.GetBorderSize(), this.BorderDashStyle, this.ShadowColor, this.ShadowOffset, PenAlignment.Inset);
						base.Common.Chart.CallOnPrePaint(new ChartPaintEventArgs(this, chartGraph, base.Common, this.Position));
					}
					if (base.Common.ProcessModeRegions)
					{
						this.SelectLegendBackground();
					}
					this.DrawLegendHeader(chartGraph);
					this.DrawLegendTitle(chartGraph);
					if (base.Common.ProcessModeRegions && !this._titlePosition.IsEmpty)
					{
						base.Common.HotRegionsList.AddHotRegion(chartGraph.GetRelativeRectangle(this._titlePosition), this, ChartElementType.LegendTitle, true);
					}
					if (this._numberOfLegendItemsToProcess < 0)
					{
						this._numberOfLegendItemsToProcess = this.legendItems.Count;
					}
					for (int i = 0; i < this._numberOfLegendItemsToProcess; i++)
					{
						LegendItem item2 = this.legendItems[i];
						for (int j = 0; j < item2.Cells.Count; j++)
						{
							item2.Cells[j].Paint(chartGraph, this._autoFitFontSizeAdjustment, this.autofitFont, this.singleWCharacterSize);
						}
						if ((item2.SeparatorType == LegendSeparatorStyle.None) || (item2.Cells.Count <= 0))
						{
							goto Label_0371;
						}
						Rectangle empty = Rectangle.Empty;
						empty.X = item2.Cells[0].cellPosition.Left;
						int right = 0;
						for (int k = item2.Cells.Count - 1; k >= 0; k--)
						{
							right = item2.Cells[k].cellPosition.Right;
							if (right > 0)
							{
								goto Label_0304;
							}
						}
					Label_0304:
						empty.Width = right - empty.X;
						empty.Y = item2.Cells[0].cellPosition.Bottom;
						empty.Height = this.GetSeparatorSize(item2.SeparatorType).Height;
						empty.Intersect(this._legendItemsAreaPosition);
						this.DrawSeparator(chartGraph, item2.SeparatorType, item2.SeparatorColor, true, empty);
					Label_0371: ;
					}
					if (this.ItemColumnSeparator != LegendSeparatorStyle.None)
					{
						Rectangle position = Rectangle.Round(chartGraph.GetAbsoluteRectangle(this.Position.ToRectangleF()));
						position.Y += this.GetBorderSize() + this._titlePosition.Height;
						position.Height -= (2 * this.GetBorderSize()) + this._titlePosition.Height;
						position.X += this.GetBorderSize() + this._offset.Width;
						position.Width = this.GetSeparatorSize(this.ItemColumnSeparator).Width;
						if (this._horizontalSpaceLeft > 0)
						{
							position.X += this._horizontalSpaceLeft / 2;
						}
						if ((position.Width > 0) && (position.Height > 0))
						{
							for (int m = 0; m < this._itemColumns; m++)
							{
								int numberOfCells = this.GetNumberOfCells();
								for (int n = 0; n < numberOfCells; n++)
								{
									position.X += this._subColumnSizes[m, n];
								}
								if (m < (this._itemColumns - 1))
								{
									this.DrawSeparator(chartGraph, this.ItemColumnSeparator, this.ItemColumnSeparatorColor, false, position);
								}
								position.X += position.Width;
							}
						}
					}
					if (this._legendItemsTruncated && (this._legendItemsAreaPosition.Height > (this._truncatedDotsSize / 2)))
					{
						int num8 = 3;
						int num9 = (this._legendItemsAreaPosition.Width / 3) / num8;
						num9 = Math.Min(num9, 10);
						PointF point = new PointF((this._legendItemsAreaPosition.X + (this._legendItemsAreaPosition.Width / 2)) - (num9 * ((float)Math.Floor((double)(((float)num8) / 2f)))), (float)(this._legendItemsAreaPosition.Bottom + ((this._truncatedDotsSize + this._offset.Height) / 2)));
						for (int num10 = 0; num10 < num8; num10++)
						{
							chartGraph.DrawMarkerRel(chartGraph.GetRelativePoint(point), MarkerStyle.Circle, this._truncatedDotsSize, this.ForeColor, Color.Empty, 0, string.Empty, Color.Empty, 0, Color.Empty, RectangleF.Empty);
							point.X += num9;
						}
					}
					if (base.Common.ProcessModePaint)
					{
						base.Common.Chart.CallOnPostPaint(new ChartPaintEventArgs(this, chartGraph, base.Common, this.Position));
					}
					foreach (LegendItem item3 in this.legendItems)
					{
						if (item3.clearTempCells)
						{
							item3.clearTempCells = false;
							item3.Cells.Clear();
						}
					}
				}
			}
		}

		private void RecalcLegendInfo(ChartGraphics chartGraph)
		{
			RectangleF ef = this._position.ToRectangleF();
			Rectangle rectangle = Rectangle.Round(chartGraph.GetAbsoluteRectangle(ef));
			this.singleWCharacterSize = chartGraph.MeasureStringAbs("W", this.Font);
			Size size = chartGraph.MeasureStringAbs("WW", this.Font);
			this.singleWCharacterSize.Width = size.Width - this.singleWCharacterSize.Width;
			this._offset.Width = (int)Math.Ceiling((double)(((float)this.singleWCharacterSize.Width) / 2f));
			this._offset.Height = (int)Math.Ceiling((double)(((float)this.singleWCharacterSize.Width) / 3f));
			this._itemColumnSpacingRel = (int)(this.singleWCharacterSize.Width * (((float)this._itemColumnSpacing) / 100f));
			if ((this._itemColumnSpacingRel % 2) == 1)
			{
				this._itemColumnSpacingRel++;
			}
			this._titlePosition = Rectangle.Empty;
			if (this.Title.Length > 0)
			{
				Size titleSize = this.GetTitleSize(chartGraph, rectangle.Size);
				this._titlePosition = new Rectangle(rectangle.Location.X, rectangle.Location.Y, rectangle.Width, Math.Min(rectangle.Height, titleSize.Height));
				rectangle.Height -= this._titlePosition.Height;
				this._titlePosition.Y += this.GetBorderSize();
			}
			this._headerPosition = Rectangle.Empty;
			Size empty = Size.Empty;
			foreach (LegendCellColumn column in this.CellColumns)
			{
				if (column.HeaderText.Length > 0)
				{
					Size headerSize = this.GetHeaderSize(chartGraph, column);
					empty.Height = Math.Max(empty.Height, headerSize.Height);
				}
			}
			if (!empty.IsEmpty)
			{
				this._headerPosition = new Rectangle((rectangle.Location.X + this.GetBorderSize()) + this._offset.Width, rectangle.Location.Y + this._titlePosition.Height, rectangle.Width - ((this.GetBorderSize() + this._offset.Width) * 2), Math.Min(rectangle.Height - this._titlePosition.Height, empty.Height));
				this._headerPosition.Height = Math.Max(this._headerPosition.Height, 0);
				rectangle.Height -= this._headerPosition.Height;
				this._headerPosition.Y += this.GetBorderSize();
			}
			this._legendItemsAreaPosition = new Rectangle((rectangle.X + this._offset.Width) + this.GetBorderSize(), (((rectangle.Y + this._offset.Height) + this.GetBorderSize()) + this._titlePosition.Height) + this._headerPosition.Height, rectangle.Width - (2 * (this._offset.Width + this.GetBorderSize())), rectangle.Height - (2 * (this._offset.Height + this.GetBorderSize())));
			this.GetNumberOfRowsAndColumns(chartGraph, this._legendItemsAreaPosition.Size, -1, out this._numberOfRowsPerColumn, out this._itemColumns, out this._horizontalSpaceLeft, out this._verticalSpaceLeft);
			this._autoFitFontSizeAdjustment = 0;
			this._legendItemsTruncated = false;
			bool flag = (this._horizontalSpaceLeft >= 0) && (this._verticalSpaceLeft >= 0);
			this._numberOfLegendItemsToProcess = this.legendItems.Count;
			int num = 0;
			for (int i = 0; i < this._itemColumns; i++)
			{
				num += this._numberOfRowsPerColumn[i];
			}
			if (num < this._numberOfLegendItemsToProcess)
			{
				flag = false;
			}
			this.autofitFont = this.Font;
			if (!flag)
			{
				do
				{
					if (this.IsTextAutoFit && ((this.Font.Size - this._autoFitFontSizeAdjustment) > this._autoFitMinFontSize))
					{
						this._autoFitFontSizeAdjustment++;
						int num3 = (int)Math.Round((double)(this.Font.Size - this._autoFitFontSizeAdjustment));
						if (num3 < 1)
						{
							num3 = 1;
						}
						this.autofitFont = base.Common.ChartPicture.FontCache.GetFont(this.Font.FontFamily, (float)num3, this.Font.Style, this.Font.Unit);
						this.GetNumberOfRowsAndColumns(chartGraph, this._legendItemsAreaPosition.Size, -1, out this._numberOfRowsPerColumn, out this._itemColumns, out this._horizontalSpaceLeft, out this._verticalSpaceLeft);
						flag = (this._horizontalSpaceLeft >= 0) && (this._verticalSpaceLeft >= 0);
						num = 0;
						for (int k = 0; k < this._itemColumns; k++)
						{
							num += this._numberOfRowsPerColumn[k];
						}
						if (num < this._numberOfLegendItemsToProcess)
						{
							flag = false;
						}
					}
					else
					{
						if (this._numberOfLegendItemsToProcess > 2)
						{
							if (((this._itemColumns == 1) && (this._horizontalSpaceLeft < 0)) && (this._verticalSpaceLeft >= 0))
							{
								flag = true;
								this._numberOfLegendItemsToProcess = Math.Min(this._numberOfLegendItemsToProcess, this._numberOfRowsPerColumn[0]);
							}
							else if (((this.GetMaximumNumberOfRows() == 1) && (this._verticalSpaceLeft < 0)) && (this._horizontalSpaceLeft >= 0))
							{
								flag = true;
								this._numberOfLegendItemsToProcess = Math.Min(this._numberOfLegendItemsToProcess, this._itemColumns);
							}
							else
							{
								if (!this._legendItemsTruncated)
								{
									this._legendItemsAreaPosition.Height -= this._truncatedDotsSize;
								}
								this._legendItemsTruncated = true;
								this._numberOfLegendItemsToProcess--;
								this.GetNumberOfRowsAndColumns(chartGraph, this._legendItemsAreaPosition.Size, this._numberOfLegendItemsToProcess, out this._numberOfRowsPerColumn, out this._itemColumns);
							}
							if ((flag && !this._legendItemsTruncated) && (this._numberOfLegendItemsToProcess < this.legendItems.Count))
							{
								this._legendItemsAreaPosition.Height -= this._truncatedDotsSize;
								this._legendItemsTruncated = true;
							}
						}
						else
						{
							flag = true;
						}
						if (!flag)
						{
							flag = this.CheckLegendItemsFit(chartGraph, this._legendItemsAreaPosition.Size, this._numberOfLegendItemsToProcess, this._autoFitFontSizeAdjustment, this._itemColumns, this._numberOfRowsPerColumn, out this._subColumnSizes, out this._cellHeights, out this._horizontalSpaceLeft, out this._verticalSpaceLeft);
						}
					}
				}
				while (!flag);
			}
			Size itemHalfSpacing = Size.Empty;
			if (this._verticalSpaceLeft > 0)
			{
				itemHalfSpacing.Height = (this._verticalSpaceLeft / this.GetMaximumNumberOfRows()) / 2;
			}
			if (this._horizontalSpaceLeft > 0)
			{
				itemHalfSpacing.Width = this._horizontalSpaceLeft / 2;
			}
			int columnIndex = 0;
			int rowIndex = 0;
			if (this._numberOfLegendItemsToProcess < 0)
			{
				this._numberOfLegendItemsToProcess = this.legendItems.Count;
			}
			for (int j = 0; j < this._numberOfLegendItemsToProcess; j++)
			{
				LegendItem item = this.legendItems[j];
				for (int m = 0; m < item.Cells.Count; m++)
				{
					LegendCell cell = item.Cells[m];
					Rectangle position = this.GetCellPosition(columnIndex, rowIndex, m, itemHalfSpacing);
					int num9 = 0;
					if (cell.CellSpan > 1)
					{
						for (int n = 1; (n < cell.CellSpan) && ((m + n) < item.Cells.Count); n++)
						{
							Rectangle rectangle3 = this.GetCellPosition(columnIndex, rowIndex, m + n, itemHalfSpacing);
							if (position.Right < rectangle3.Right)
							{
								position.Width += rectangle3.Right - position.Right;
							}
							num9++;
							item.Cells[m + n].SetCellPosition(rowIndex, Rectangle.Empty, this.singleWCharacterSize);
						}
					}
					position.Intersect(this._legendItemsAreaPosition);
					cell.SetCellPosition(rowIndex, position, this.singleWCharacterSize);
					m += num9;
				}
				rowIndex++;
				if (rowIndex >= this._numberOfRowsPerColumn[columnIndex])
				{
					columnIndex++;
					rowIndex = 0;
					if (columnIndex >= this._itemColumns)
					{
						return;
					}
				}
			}
		}

		internal void SelectLegendBackground()
		{
			base.Common.HotRegionsList.AddHotRegion(this.Position.ToRectangleF(), this, ChartElementType.LegendArea, true);
		}

		internal bool ShouldSerializePosition()
		{
			return !this.Position.Auto;
		}

		[SRCategory("CategoryAttributeDocking"), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(0), SRDescription("DescriptionAttributeLegend_Alignment"), NotifyParentProperty(true)]
		public StringAlignment Alignment
		{
			get
			{
				return this._legendAlignment;
			}
			set
			{
				this._legendAlignment = value;
				this.Invalidate(false);
			}
		}

		[DefaultValue(7), SRDescription("DescriptionAttributeLegend_AutoFitMinFontSize"), SRCategory("CategoryAttributeAppearance")]
		public int AutoFitMinFontSize
		{
			get
			{
				return this._autoFitMinFontSize;
			}
			set
			{
				if (value < 5)
				{
					throw new InvalidOperationException(SR.ExceptionLegendAutoFitMinFontSizeInvalid);
				}
				this._autoFitMinFontSize = value;
				this.Invalidate(false);
			}
		}

		[DefaultValue(typeof(Color), ""), Bindable(true), NotifyParentProperty(true), TypeConverter(typeof(ColorConverter)), Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), PersistenceMode(PersistenceMode.Attribute), SRDescription("DescriptionAttributeBackColor"), SRCategory("CategoryAttributeAppearance")]
		public Color BackColor
		{
			get
			{
				return this._backColor;
			}
			set
			{
				this._backColor = value;
				this.Invalidate(false);
			}
		}

		[PersistenceMode(PersistenceMode.Attribute), SRDescription("DescriptionAttributeBackGradientStyle"), Editor(typeof(GradientEditor), typeof(UITypeEditor)), SRCategory("CategoryAttributeAppearance"), Bindable(true), DefaultValue(0), NotifyParentProperty(true)]
		public GradientStyle BackGradientStyle
		{
			get
			{
				return this._backGradientStyle;
			}
			set
			{
				this._backGradientStyle = value;
				this.Invalidate(true);
			}
		}

		[DefaultValue(0), PersistenceMode(PersistenceMode.Attribute), Editor(typeof(HatchStyleEditor), typeof(UITypeEditor)), Bindable(true), SRCategory("CategoryAttributeAppearance"), NotifyParentProperty(true), SRDescription("DescriptionAttributeBackHatchStyle")]
		public ChartHatchStyle BackHatchStyle
		{
			get
			{
				return this._backHatchStyle;
			}
			set
			{
				this._backHatchStyle = value;
				this.Invalidate(true);
			}
		}

		[NotifyParentProperty(true), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(""), SRDescription("DescriptionAttributeBackImage"), Editor(typeof(ImageValueEditor), typeof(UITypeEditor)), SRCategory("CategoryAttributeAppearance")]
		public string BackImage
		{
			get
			{
				return this._backImage;
			}
			set
			{
				this._backImage = value;
				this.Invalidate(true);
			}
		}

		[SRCategory("CategoryAttributeAppearance"), NotifyParentProperty(true), SRDescription("DescriptionAttributeBackImageAlign"), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(0)]
		public ChartImageAlignmentStyle BackImageAlignment
		{
			get
			{
				return this._backImageAlignment;
			}
			set
			{
				this._backImageAlignment = value;
				this.Invalidate(true);
			}
		}

		[SRCategory("CategoryAttributeAppearance"), PersistenceMode(PersistenceMode.Attribute), Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), Bindable(true), DefaultValue(typeof(Color), ""), NotifyParentProperty(true), SRDescription("DescriptionAttributeImageTransparentColor"), TypeConverter(typeof(ColorConverter))]
		public Color BackImageTransparentColor
		{
			get
			{
				return this._backImageTransparentColor;
			}
			set
			{
				this._backImageTransparentColor = value;
				this.Invalidate(true);
			}
		}

		[SRCategory("CategoryAttributeAppearance"), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(0), NotifyParentProperty(true), SRDescription("DescriptionAttributeImageWrapMode")]
		public ChartImageWrapMode BackImageWrapMode
		{
			get
			{
				return this._backImageWrapMode;
			}
			set
			{
				this._backImageWrapMode = value;
				this.Invalidate(true);
			}
		}

		[SRDescription("DescriptionAttributeBackSecondaryColor"), SRCategory("CategoryAttributeAppearance"), TypeConverter(typeof(ColorConverter)), Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(typeof(Color), ""), NotifyParentProperty(true)]
		public Color BackSecondaryColor
		{
			get
			{
				return this._backSecondaryColor;
			}
			set
			{
				this._backSecondaryColor = value;
				this.Invalidate(true);
			}
		}

		[Bindable(true), SRDescription("DescriptionAttributeBorderColor"), NotifyParentProperty(true), TypeConverter(typeof(ColorConverter)), Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), PersistenceMode(PersistenceMode.Attribute), SRCategory("CategoryAttributeAppearance"), DefaultValue(typeof(Color), "")]
		public Color BorderColor
		{
			get
			{
				return this._borderColor;
			}
			set
			{
				this._borderColor = value;
				this.Invalidate(false);
			}
		}

		[NotifyParentProperty(true), SRDescription("DescriptionAttributeBorderDashStyle"), SRCategory("CategoryAttributeAppearance"), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(5)]
		public ChartDashStyle BorderDashStyle
		{
			get
			{
				return this._borderDashStyle;
			}
			set
			{
				this._borderDashStyle = value;
				this.Invalidate(false);
			}
		}

		[Bindable(true), SRDescription("DescriptionAttributeBorderWidth"), NotifyParentProperty(true), PersistenceMode(PersistenceMode.Attribute), SRCategory("CategoryAttributeAppearance"), DefaultValue(1)]
		public int BorderWidth
		{
			get
			{
				return this._borderWidth;
			}
			set
			{
				if (value < 0)
				{
					throw new ArgumentOutOfRangeException("value", SR.ExceptionLegendBorderWidthIsNegative);
				}
				this._borderWidth = value;
				this.Invalidate(false);
			}
		}

		[PersistenceMode(PersistenceMode.InnerProperty), Editor(typeof(LegendCellColumnCollectionEditor), typeof(UITypeEditor)), SRCategory("CategoryAttributeCellColumns"), SRDescription("DescriptionAttributeLegend_CellColumns")]
		public LegendCellColumnCollection CellColumns
		{
			get
			{
				return this._cellColumns;
			}
		}

		[SRDescription("DescriptionAttributeLegend_CustomItems"), Editor(typeof(LegendItemCollectionEditor), typeof(UITypeEditor)), SRCategory("CategoryAttributeAppearance"), Bindable(true), NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty)]
		public LegendItemsCollection CustomItems
		{
			get
			{
				return this._customLegends;
			}
		}

		[PersistenceMode(PersistenceMode.Attribute), SRCategory("CategoryAttributeDocking"), Bindable(true), DefaultValue("NotSet"), SRDescription("DescriptionAttributeLegend_DockToChartArea"), TypeConverter(typeof(LegendAreaNameConverter)), NotifyParentProperty(true)]
		public string DockedToChartArea
		{
			get
			{
				return this._dockedToChartArea;
			}
			set
			{
				if (value != this._dockedToChartArea)
				{
					if (string.IsNullOrEmpty(value))
					{
						this._dockedToChartArea = "NotSet";
					}
					else
					{
						if ((base.Chart != null) && (base.Chart.ChartAreas != null))
						{
							base.Chart.ChartAreas.VerifyNameReference(value);
						}
						this._dockedToChartArea = value;
					}
					this.Invalidate(false);
				}
			}
		}

		[PersistenceMode(PersistenceMode.Attribute), NotifyParentProperty(true), SRDescription("DescriptionAttributeLegend_Docking"), SRCategory("CategoryAttributeDocking"), Bindable(true), DefaultValue(1)]
		public Docking Docking
		{
			get
			{
				return this._legendDocking;
			}
			set
			{
				this._legendDocking = value;
				this.Invalidate(false);
			}
		}

		[ParenthesizePropertyName(true), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(true), SRDescription("DescriptionAttributeLegend_Enabled"), NotifyParentProperty(true), SRCategory("CategoryAttributeAppearance")]
		public bool Enabled
		{
			get
			{
				return this._enabled;
			}
			set
			{
				this._enabled = value;
				this.Invalidate(false);
			}
		}

		[NotifyParentProperty(true), Bindable(true), DefaultValue(typeof(System.Drawing.Font), "Microsoft Sans Serif, 8pt"), SRDescription("DescriptionAttributeLegend_Font"), SRCategory("CategoryAttributeAppearance"), PersistenceMode(PersistenceMode.Attribute)]
		public System.Drawing.Font Font
		{
			get
			{
				return this._font;
			}
			set
			{
				this.IsTextAutoFit = false;
				this._font = value;
				this.Invalidate(false);
			}
		}

		[DefaultValue(typeof(Color), "Black"), TypeConverter(typeof(ColorConverter)), Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), Bindable(true), SRCategory("CategoryAttributeAppearance"), SRDescription("DescriptionAttributeLegendFontColor"), NotifyParentProperty(true), PersistenceMode(PersistenceMode.Attribute)]
		public Color ForeColor
		{
			get
			{
				return this._foreColor;
			}
			set
			{
				this._foreColor = value;
				this.Invalidate(true);
			}
		}

		[DefaultValue(typeof(LegendSeparatorStyle), "None"), SRDescription("DescriptionAttributeLegend_HeaderSeparator"), SRCategory("CategoryAttributeCellColumns")]
		public LegendSeparatorStyle HeaderSeparator
		{
			get
			{
				return this._headerSeparator;
			}
			set
			{
				if (value != this._headerSeparator)
				{
					this._headerSeparator = value;
					this.Invalidate(false);
				}
			}
		}

		[TypeConverter(typeof(ColorConverter)), Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), DefaultValue(typeof(Color), "Black"), SRDescription("DescriptionAttributeLegend_HeaderSeparatorColor"), SRCategory("CategoryAttributeCellColumns")]
		public Color HeaderSeparatorColor
		{
			get
			{
				return this._headerSeparatorColor;
			}
			set
			{
				if (value != this._headerSeparatorColor)
				{
					this._headerSeparatorColor = value;
					this.Invalidate(false);
				}
			}
		}

		[SerializationVisibility(SerializationVisibility.Hidden), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), TypeConverter(typeof(LegendAreaNameConverter)), SRCategory("CategoryAttributeAppearance"), Browsable(false), Bindable(false), DefaultValue("NotSet"), NotifyParentProperty(true), SRDescription("DescriptionAttributeLegend_InsideChartArea"), EditorBrowsable(EditorBrowsableState.Never)]
		public string InsideChartArea
		{
			get
			{
				if (((base.Common != null) && (base.Common.Chart != null)) && base.Common.Chart.Serializing)
				{
					return "NotSet";
				}
				return this.DockedToChartArea;
			}
			set
			{
				if (value.Length == 0)
				{
					this.DockedToChartArea = "NotSet";
				}
				else
				{
					this.DockedToChartArea = value;
				}
				this.Invalidate(false);
			}
		}

		[SRDescription("DescriptionAttributeLegend_InterlacedRows"), SRCategory("CategoryAttributeAppearance"), DefaultValue(false)]
		public bool InterlacedRows
		{
			get
			{
				return this._interlacedRows;
			}
			set
			{
				if (value != this._interlacedRows)
				{
					this._interlacedRows = value;
					this.Invalidate(false);
				}
			}
		}

		[Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(ColorConverter)), SRCategory("CategoryAttributeAppearance"), DefaultValue(typeof(Color), ""), SRDescription("DescriptionAttributeLegend_InterlacedRowsColor")]
		public Color InterlacedRowsColor
		{
			get
			{
				return this._interlacedRowsColor;
			}
			set
			{
				if (value != this._interlacedRowsColor)
				{
					this._interlacedRowsColor = value;
					this.Invalidate(false);
				}
			}
		}

		[DefaultValue(true), Bindable(true), SRDescription("DescriptionAttributeLegend_DockInsideChartArea"), NotifyParentProperty(true), PersistenceMode(PersistenceMode.Attribute), SRCategory("CategoryAttributeDocking")]
		public bool IsDockedInsideChartArea
		{
			get
			{
				return this._isDockedInsideChartArea;
			}
			set
			{
				if (value != this._isDockedInsideChartArea)
				{
					this._isDockedInsideChartArea = value;
					this.Invalidate(false);
				}
			}
		}

		[SRCategory("CategoryAttributeAppearance"), DefaultValue(false), SRDescription("DescriptionAttributeLegend_EquallySpacedItems"), NotifyParentProperty(true), PersistenceMode(PersistenceMode.Attribute), Bindable(true)]
		public bool IsEquallySpacedItems
		{
			get
			{
				return this._isEquallySpacedItems;
			}
			set
			{
				this._isEquallySpacedItems = value;
				this.Invalidate(false);
			}
		}

		[NotifyParentProperty(true), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(true), SRDescription("DescriptionAttributeLegend_AutoFitText"), SRCategory("CategoryAttributeAppearance")]
		public bool IsTextAutoFit
		{
			get
			{
				return this._isTextAutoFit;
			}
			set
			{
				this._isTextAutoFit = value;
				if (this._isTextAutoFit)
				{
					if (this._font != null)
					{
						this._font = this._fontCache.GetFont(this._font.FontFamily, 8f, this._font.Style);
					}
					else
					{
						this._font = this._fontCache.DefaultFont;
					}
				}
				this.Invalidate(false);
			}
		}

		[SRDescription("DescriptionAttributeLegend_ItemColumnSeparator"), SRCategory("CategoryAttributeCellColumns"), DefaultValue(typeof(LegendSeparatorStyle), "None")]
		public LegendSeparatorStyle ItemColumnSeparator
		{
			get
			{
				return this._itemColumnSeparator;
			}
			set
			{
				if (value != this._itemColumnSeparator)
				{
					this._itemColumnSeparator = value;
					this.Invalidate(false);
				}
			}
		}

		[DefaultValue(typeof(Color), "Black"), SRCategory("CategoryAttributeCellColumns"), TypeConverter(typeof(ColorConverter)), SRDescription("DescriptionAttributeLegend_ItemColumnSeparatorColor"), Editor(typeof(ChartColorEditor), typeof(UITypeEditor))]
		public Color ItemColumnSeparatorColor
		{
			get
			{
				return this._itemColumnSeparatorColor;
			}
			set
			{
				if (value != this._itemColumnSeparatorColor)
				{
					this._itemColumnSeparatorColor = value;
					this.Invalidate(false);
				}
			}
		}

		[SRDescription("DescriptionAttributeLegend_ItemColumnSpacing"), SRCategory("CategoryAttributeCellColumns"), DefaultValue(50)]
		public int ItemColumnSpacing
		{
			get
			{
				return this._itemColumnSpacing;
			}
			set
			{
				if (value != this._itemColumnSpacing)
				{
					if (value < 0)
					{
						throw new ArgumentOutOfRangeException("value", SR.ExceptionLegendColumnSpacingInvalid);
					}
					this._itemColumnSpacing = value;
					this.Invalidate(false);
				}
			}
		}

		[SRCategory("CategoryAttributeAppearance"), DefaultValue(0), SRDescription("DescriptionAttributeLegend_Reversed")]
		public LegendItemOrder LegendItemOrder
		{
			get
			{
				return this._legendItemOrder;
			}
			set
			{
				if (value != this._legendItemOrder)
				{
					this._legendItemOrder = value;
					this.Invalidate(false);
				}
			}
		}

		[SRCategory("CategoryAttributeAppearance"), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(2), SRDescription("DescriptionAttributeLegend_LegendStyle"), NotifyParentProperty(true), ParenthesizePropertyName(true)]
		public LegendStyle LegendStyle
		{
			get
			{
				return this._legendStyle;
			}
			set
			{
				this._legendStyle = value;
				this.Invalidate(false);
			}
		}

		[SRDescription("DescriptionAttributeLegend_MaxAutoSize"), SRCategory("CategoryAttributeDocking"), DefaultValue((float)50f)]
		public float MaximumAutoSize
		{
			get
			{
				return this._maximumLegendAutoSize;
			}
			set
			{
				if ((value < 0f) || (value > 100f))
				{
					throw new ArgumentOutOfRangeException("value", SR.ExceptionLegendMaximumAutoSizeInvalid);
				}
				this._maximumLegendAutoSize = value;
				this.Invalidate(false);
			}
		}

		[PersistenceMode(PersistenceMode.Attribute), SRCategory("CategoryAttributeMisc"), Bindable(true), SRDescription("DescriptionAttributeLegend_Name"), NotifyParentProperty(true)]
		public override string Name
		{
			get
			{
				return base.Name;
			}
			set
			{
				base.Name = value;
			}
		}

		[NotifyParentProperty(true), Bindable(true), PersistenceMode(PersistenceMode.InnerProperty), TypeConverter(typeof(ElementPositionConverter)), SerializationVisibility(SerializationVisibility.Element), SRDescription("DescriptionAttributeLegend_Position"), SRCategory("CategoryAttributeAppearance")]
		public ElementPosition Position
		{
			get
			{
				if (((base.Common == null) || (base.Common.Chart == null)) || (base.Common.Chart.SerializationStatus != SerializationStatus.Saving))
				{
					return this._position;
				}
				if (this._position.Auto)
				{
					return new ElementPosition();
				}
				ElementPosition position = new ElementPosition();
				position.Auto = true;
				position.SetPositionNoAuto(this._position.X, this._position.Y, this._position.Width, this._position.Height);
				return position;
			}
			set
			{
				this._position = value;
				this.Invalidate(false);
			}
		}

		[NotifyParentProperty(true), PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue(typeof(Color), "128, 0, 0, 0"), SRDescription("DescriptionAttributeShadowColor"), SRCategory("CategoryAttributeAppearance"), TypeConverter(typeof(ColorConverter)), Editor(typeof(ChartColorEditor), typeof(UITypeEditor))]
		public Color ShadowColor
		{
			get
			{
				return this._shadowColor;
			}
			set
			{
				this._shadowColor = value;
				this.Invalidate(true);
			}
		}

		[SRCategory("CategoryAttributeAppearance"), NotifyParentProperty(true), Bindable(true), DefaultValue(0), SRDescription("DescriptionAttributeShadowOffset"), PersistenceMode(PersistenceMode.Attribute)]
		public int ShadowOffset
		{
			get
			{
				return this._shadowOffset;
			}
			set
			{
				this._shadowOffset = value;
				this.Invalidate(false);
			}
		}

		[Bindable(true), SRDescription("DescriptionAttributeLegend_TableStyle"), PersistenceMode(PersistenceMode.Attribute), DefaultValue(0), SRCategory("CategoryAttributeAppearance"), NotifyParentProperty(true), ParenthesizePropertyName(true)]
		public LegendTableStyle TableStyle
		{
			get
			{
				return this._legendTableStyle;
			}
			set
			{
				this._legendTableStyle = value;
				this.Invalidate(false);
			}
		}

		[DefaultValue(0x19), SRDescription("DescriptionAttributeLegend_TextWrapThreshold"), SRCategory("CategoryAttributeAppearance")]
		public int TextWrapThreshold
		{
			get
			{
				return this._textWrapThreshold;
			}
			set
			{
				if (value != this._textWrapThreshold)
				{
					if (value < 0)
					{
						throw new ArgumentException(SR.ExceptionTextThresholdIsNegative, "value");
					}
					this._textWrapThreshold = value;
					this.Invalidate(false);
				}
			}
		}

		[DefaultValue(""), SRDescription("DescriptionAttributeLegend_Title"), SRCategory("CategoryAttributeTitle")]
		public string Title
		{
			get
			{
				return this._title;
			}
			set
			{
				if (value != this._title)
				{
					this._title = value;
					this.Invalidate(false);
				}
			}
		}

		[SRDescription("DescriptionAttributeLegend_TitleAlignment"), SRCategory("CategoryAttributeTitle"), DefaultValue(typeof(StringAlignment), "Center")]
		public StringAlignment TitleAlignment
		{
			get
			{
				return this._titleAlignment;
			}
			set
			{
				if (value != this._titleAlignment)
				{
					this._titleAlignment = value;
					this.Invalidate(false);
				}
			}
		}

		[TypeConverter(typeof(ColorConverter)), Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), DefaultValue(typeof(Color), ""), SRDescription("DescriptionAttributeTitleBackColor"), SRCategory("CategoryAttributeTitle")]
		public Color TitleBackColor
		{
			get
			{
				return this._titleBackColor;
			}
			set
			{
				if (value != this._titleBackColor)
				{
					this._titleBackColor = value;
					this.Invalidate(false);
				}
			}
		}

		[DefaultValue(typeof(System.Drawing.Font), "Microsoft Sans Serif, 8pt, style=Bold"), SRDescription("DescriptionAttributeTitleFont"), SRCategory("CategoryAttributeTitle")]
		public System.Drawing.Font TitleFont
		{
			get
			{
				return this._titleFont;
			}
			set
			{
				if (value != this._titleFont)
				{
					this._titleFont = value;
					this.Invalidate(false);
				}
			}
		}

		[TypeConverter(typeof(ColorConverter)), SRCategory("CategoryAttributeTitle"), Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), DefaultValue(typeof(Color), "Black"), SRDescription("DescriptionAttributeLegend_TitleColor")]
		public Color TitleForeColor
		{
			get
			{
				return this._titleForeColor;
			}
			set
			{
				if (value != this._titleForeColor)
				{
					this._titleForeColor = value;
					this.Invalidate(false);
				}
			}
		}

		[SRDescription("DescriptionAttributeLegend_TitleSeparator"), SRCategory("CategoryAttributeTitle"), DefaultValue(typeof(LegendSeparatorStyle), "None")]
		public LegendSeparatorStyle TitleSeparator
		{
			get
			{
				return this._titleSeparator;
			}
			set
			{
				if (value != this._titleSeparator)
				{
					this._titleSeparator = value;
					this.Invalidate(false);
				}
			}
		}

		[SRDescription("DescriptionAttributeLegend_TitleSeparatorColor"), SRCategory("CategoryAttributeTitle"), DefaultValue(typeof(Color), "Black"), Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(ColorConverter))]
		public Color TitleSeparatorColor
		{
			get
			{
				return this._titleSeparatorColor;
			}
			set
			{
				if (value != this._titleSeparatorColor)
				{
					this._titleSeparatorColor = value;
					this.Invalidate(false);
				}
			}
		}
	}
}

